package com.wenyu.wenyuyuedu.adapter.personage;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.base.ListBaseAdapter;
import com.wenyu.wenyuyuedu.bean.personage.MytrendsBean;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/3/23.
 * 我的发表
 */

public class MyPublicAdapter extends ListBaseAdapter<MytrendsBean.InfoBean> {

    private Context context;
    private MyViewHolder holderView;
    private MyCollectAdapter.deleteOnclickListener deleteOnclickListener;

    public void setDeleteOnClickLis(MyCollectAdapter.deleteOnclickListener deleteOnClickLis) {
        this.deleteOnclickListener = deleteOnClickLis;

    }

    public MyPublicAdapter(Context mContext) {
        super(mContext);
        this.context = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.my_public_item, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MytrendsBean.InfoBean infoBean = mDataList.get(position);
        holderView = (MyViewHolder) holder;
        if (infoBean != null) {
                    /*获取你年月日*/
            String create_time = infoBean.getCreate_time();
            long l = Long.valueOf(create_time) * 1000;
            Date date = new Date(l);
        /*日*/
            int day = date.getDate();
            if(day < 10){
                holderView.mycollect_data.setText( "0" + day + "");
            }else {
                holderView.mycollect_data.setText( day + "");
            }

        /*月*/
            int month = date.getMonth() + 1;
            holderView.mycollect_mouth.setText(month + "月");
            String content = infoBean.getContent();
            if (content.equals("")) {

            }
        /*获取帖子的内容*/
            String content1 = infoBean.getContent();
            if(content1.equals("")){
                holderView.home_all_content.setVisibility(View.GONE);
                holderView.home_all_content.setText(infoBean.getContent());
            }else {
                holderView.home_all_content.setVisibility(View.VISIBLE);
                holderView.home_all_content.setText(infoBean.getContent());
            }

            /*书圈的标题*/
            holderView.mycollect_circle_name.setText(infoBean.getTitle());
            /*填充内容的图片*/
            fillContentPhoto(infoBean);
            holderView.btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (deleteOnclickListener != null) {
                        deleteOnclickListener.onClick(position, view);
                    }
                }
            });
            holderView.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (deleteOnclickListener != null) {
                        deleteOnclickListener.onClick(position, view);
                    }
                }
            });
        }
    }

    public List<MytrendsBean.InfoBean> getList() {
        return mDataList;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout tv;
        private Button btn_delete;
        private TextView mycollect_data;
        private TextView mycollect_mouth;
        private ImageView contentOne;
        private ImageView contentTwo;
        private ImageView contentThree;
        private ImageView contentFour;
        private ImageView contentFive;
        private ImageView iv_allattention_transparency;
        private TextView pictureCount;
        private TextView mycollect_circle_name;
        private TextView home_all_content;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (RelativeLayout) itemView.findViewById(R.id.tv);
            btn_delete = (Button) itemView.findViewById(R.id.btn_delete);
            mycollect_data = (TextView) itemView.findViewById(R.id.mycollect_data);
            mycollect_mouth = (TextView) itemView.findViewById(R.id.mycollect_mouth);
            home_all_content = (TextView) itemView.findViewById(R.id.home_all_content);
            contentOne = (ImageView) itemView.findViewById(R.id.home_all_content_picture_one);
            contentTwo = (ImageView) itemView.findViewById(R.id.home_all_content_picture_two);
            contentThree = (ImageView) itemView.findViewById(R.id.home_all_content_picture_three);
            contentFour = (ImageView) itemView.findViewById(R.id.home_all_content_picture_four);
            contentFive = (ImageView) itemView.findViewById(R.id.home_all_content_picture_five);
            iv_allattention_transparency = (ImageView) itemView.findViewById(R.id.iv_allattention_transparency);
            pictureCount = (TextView) itemView.findViewById(R.id.home_all_picture_count);
            mycollect_circle_name = (TextView) itemView.findViewById(R.id.mycollect_circle_name);
        }
    }

    public interface deleteOnclickListener {
        void onClick(int position, View view);
    }

    private void fillContentPhoto(MytrendsBean.InfoBean infoBean) {
        List<String> images = infoBean.getImages();
        if (images != null) {
            int size = images.size();
            if (size == 1) {
                holderView.contentOne.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(0))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentOne);
                holderView.contentTwo.setVisibility(View.GONE);
                holderView.contentThree.setVisibility(View.GONE);
                holderView.contentFour.setVisibility(View.GONE);
                holderView.contentFive.setVisibility(View.GONE);
                holderView.iv_allattention_transparency.setVisibility(View.GONE);
                holderView.pictureCount.setVisibility(View.GONE);
            } else if (size == 2) {
                holderView.contentOne.setVisibility(View.VISIBLE);
                holderView.contentTwo.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(0))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentOne);
                Glide.with(context)
                        .load(images.get(1))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentTwo);
                holderView.contentThree.setVisibility(View.GONE);
                holderView.contentFour.setVisibility(View.GONE);
                holderView.contentFive.setVisibility(View.GONE);
                holderView.iv_allattention_transparency.setVisibility(View.GONE);
                holderView.pictureCount.setVisibility(View.GONE);
            } else if (size == 3) {
                holderView.contentOne.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(0))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentOne);
                holderView.contentTwo.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(1))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentTwo);
                holderView.contentThree.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(2))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentThree);
                holderView.contentFour.setVisibility(View.GONE);
                holderView.contentFive.setVisibility(View.GONE);
                holderView.iv_allattention_transparency.setVisibility(View.GONE);
                holderView.pictureCount.setVisibility(View.GONE);
            } else if (size == 4) {
                holderView.contentOne.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(0))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentOne);
                holderView.contentTwo.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(1))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentTwo);
                holderView.contentThree.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(2))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentThree);
                holderView.contentFour.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(3))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentFour);
                holderView.contentFive.setVisibility(View.GONE);
                holderView.iv_allattention_transparency.setVisibility(View.GONE);
                holderView.pictureCount.setVisibility(View.GONE);
            } else if (size == 5) {
                holderView.contentOne.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(0))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentOne);
                holderView.contentTwo.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(1))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentTwo);
                holderView.contentThree.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(2))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentThree);
                holderView.contentFour.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(3))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentFour);
                holderView.contentFive.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(4))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentFive);
                holderView.iv_allattention_transparency.setVisibility(View.GONE);
                holderView.pictureCount.setVisibility(View.GONE);
            } else if (size > 5) {
                holderView.contentOne.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(0))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentOne);
                holderView.contentTwo.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(1))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentTwo);
                holderView.contentThree.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(2))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentThree);
                holderView.contentFour.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(3))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentFour);
                holderView.contentFive.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(images.get(4))
                        .error(R.drawable.nomal_dynamic)
                        .into(holderView.contentFive);
                holderView.iv_allattention_transparency.setVisibility(View.VISIBLE);
                holderView.pictureCount.setVisibility(View.VISIBLE);
                holderView.pictureCount.setText("+" + String.valueOf(size - 5));
            }
        }
    }

}
